/**
 * Created by Administrator on 2017/3/30.
 */
$(document).ready(function () {
    breakup.initial();
});

var breakup = {
    areaRegion: null,
    initial: function () {
        $("#loadTimeDate").datetimepicker({
            language: "zh-CN",
            autoclose: true,//选中之后自动隐藏日期选择框
            todayBtn: true,//今日按钮
            singleDatePicker: true,
            format: 'yyyy-mm-dd',
            weekStart: 1,
            startView: 2,
            minView: 2,
            forceParse: false,
        });

        //地区加载
        //

        $.getJSON('/bootstrap/js/areas/sql_areas.json', function (data) {

            /**重定义数据结构**/
            /**
             * id 键,name 名字,level 层级,parentId 父级
             */
            for (var i = 0; i < data.length; i++) {
                var area = {id: data[i].id, name: data[i].cname, level: data[i].level, parentId: data[i].upid};
                data[i] = area;
            }
            breakup.areaRegionFrom = $('.bs-chinese-region-cargoFrom').chineseRegion('source', data);//导入数据并实例化
            breakup.areaRegionTo = $('.bs-chinese-region-cargoTo').chineseRegion('source', data);//导入数据并实例化
        });

        //绑定事件
        $('#cargoFrom,#cargoTo').click(function () {
            breakup.selectArea(this);
        });

        $("#btnModel").click(function () {
            breakup.dataValidated();
        });
        //提交事件
        $(".btn-primary").click(function () {
            breakup.submitData();
        });
    },
    //数据验证
    dataValidated: function () {
        //发货地址
        var cargoFrom = $("#cargoFrom");
        if (cargoFrom.val() == "") {
            cargoFrom.popover('toggle');
            return;
        } else {
            cargoFrom.popover('hide');
        }
        //收货地址
        var cargoTo = $("#cargoTo");
        if (cargoTo.val() == "") {
            cargoTo.popover('toggle');
            return;
        } else {
            cargoTo.popover('hide');
        }
        //货物名称
        var cargoName = $("#cargoName");
        if (cargoName.val() == "") {
            cargoName.popover('toggle');
            return;
        } else {
            cargoName.popover('hide');
        }
        //重量(吨)
        var weight = $("#weight")
        var wt = weight.val();
        if (wt == "") {
            weight.popover('toggle');
            return;
        } else {
            weight.popover('hide');
        }
        //一口价(元)
        var buyNow = $("#buyNow");
        wt = buyNow.val();
        if (wt == "") {
            buyNow.popover('toggle');
            return;
        } else {
            buyNow.popover('hide');
        }
        //装货时间
        var loadTimeDate = $("#loadTimeDate");
        wt = loadTimeDate.val();
        if (wt == "") {
            loadTimeDate.popover('toggle');
            return;
        } else {
            loadTimeDate.popover('hide');
        }

        $('#orderModal').modal('show');
    },
    //提交数据
    submitData: function () {
        var loading = $("#upPolicy").button('loading');
        var parms = {};
        var cke = $("#isManagerService").attr("checked");
        parms.cargoFrom = $("#cargoFromCode").val();
        parms.cargoTo = $("#cargoToCode").val();
        parms.cargoName = $("#cargoName").val();
        parms.packagingType = parseInt($("#packagingType").val());
        parms.vehicleType = parseInt($("#vehicleType").val());
        parms.vehicleLength = parseInt($("#vehicleLength").val());
        parms.weight = parseFloat($("#weight").val());
        parms.unit = parseInt($("#unit").val());
        parms.buyNow = parseFloat($("#buyNow").val());
        parms.loadTimePart = $("#loadTimePart").val();
        parms.loadTimeDate = $("#loadTimeDate").val();
        parms.paymentMethod = $("#paymentMethod").val();
        parms.isManagerService = (cke || cke == "checked") ? 1 : 0;
        parms.note = $("#note").val();

        var number = $("#txtNumber");
        if (number.val() == "") {
            number.attr("data-content", "不能为空！");
            number.popover('toggle');
            loading.button('reset');
            return;
        } else {
            number.popover('hide');
        }
        if (parseFloat(number.val()) > parseFloat(parms.weight)) {
            number.attr("data-content", "不能大于重量(吨)！");
            number.popover('toggle');
            loading.button('reset');
            return;
        } else {
            number.popover('hide');
        }

        parms.number = parseFloat(number.val());

        $.ajax({
            type: "POST",
            url: "/hongtu/v1/PublishGoods",
            data: JSON.stringify(parms),
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                alert(data.message);
                $('#orderModal').modal('hide');
                loading.button('reset');
            }
        });

    },
    //选择地区
    selectArea: function (txt) {
        txt.value = "选择你的地区";
        var id = txt.id;
        var x = $(txt).offset();
        var top = x.top + 34;
        var left = x.left;
        $('.bs-chinese-region-' + id).css({width: 400 + "px", top: top, left: left, position: "absolute"});
        $('#dropdown-area-' + id).show();
        if (id == "cargoFrom") {
            breakup.areaRegionFrom.on('completed.bs.chinese-region', function (e, areas) {
                if (areas == null || areas.length == 0)
                    return;
                txt.value = "";
                for (var i = 0; i < areas.length; i++) {
                    txt.value += areas[i].name;
                    $("#" + txt.id + "Code").val(areas[i].id);
                    if (i < areas.length - 1)
                        txt.value += "-";
                }
                $('#dropdown-area-' + id).hide();
                $('.bs-chinese-region-' + id).chineseRegion('onClearAreas')
            });
        }
        else {
            breakup.areaRegionTo.on('completed.bs.chinese-region', function (e, areas) {
                if (areas == null || areas.length == 0)
                    return;
                txt.value = "";
                for (var i = 0; i < areas.length; i++) {
                    txt.value += areas[i].name;
                    $("#" + txt.id + "Code").val(areas[i].id);
                    if (i < areas.length - 1)
                        txt.value += "-";
                }
                $('#dropdown-area-' + id).hide();
                $('.bs-chinese-region-' + id).chineseRegion('onClearAreas')
            });
        }
    }
}